<?php
/**
 * 后台管理员操作处理代码
 */
// 引入项目初始化文件
require '../include/init.php';
// 获取操作的标识act的值
$act = isset($_GET['act']) ? $_GET['act'] : $_POST['act'];
// 处理非法操作
$action = ['add', 'edit','del'];
if(!in_array($act, $action)){
	msg('非法操作');
}

//商品类添加
else if($act == 'add'){
	//print_r($_POST);
	extract($_POST);
	//处理数据
	$pname = trim($pname);
	$pnums = trim($pnums);

	$pubtime = trim($pubtime);
	$price = trim($price);
	$sprice = trim($sprice);
	$color = implode("-",$color);
	$size = implode("-",$size);
	$snums = trim($snums);
	$addr = trim($addr);
	$descp = trim($content);
	$files = $_FILES['files'];
	$movefiles = $_FILES['movefiles'];
	$fiarr=implode("-",$movefiles['name']);
	$dir = '../uploads';
	$types = ['jpg','jpeg','gif','png','bmp'];
	$file = uploadFile($files,$dir,$types);
	$move = upload_movefile($movefiles,$dir,$types);
	$sql = "INSERT into  tk_product(p_c_id,pname,price,thumb,descp,pubtime,addr,color,size,ifshow,sprice,snums,pnums,pimgs) values ('$parent_id','$pname','$price','$file','$descp','$pubtime','$addr','$color','$size','$show','$sprice','$snums','$pnums','$fiarr')";
	$res = mysql_query($sql);
	if(mysql_affected_rows()>0){
		msg('添加商品成功','product_list.php');
	}else{
		msg('添加商品失败！','product_list.php');
	}
}
else if($act == 'edit'){

		extract($_POST);
	//处理数据
	$pname = trim($pname);
	$pnums = trim($pnums);
	$pubtime = trim($pubtime);
	$cate_id = $parent_id;
	$price = trim($price);
	$sprice = trim($sprice);
	$color = implode("-",$color);
	$size = implode("-",$size);
	$snums = trim($snums);
	$addr = trim($addr);
	$descp = trim($content);
	$files = $_FILES['files'];
	$movefiles = $_FILES['movefiles'];
	$fiarr=implode("-",$movefiles['name']);
	$dir = '../uploads';
	$types = ['jpg','jpeg','gif','png','bmp'];
	$file = uploadFile($files,$dir,$types);
	$move = upload_movefile($movefiles,$dir,$types);

	//修改商品的数据库处理
	$sql = "UPDATE tk_product set p_c_id ='$cate_id',pname ='$pname',price ='$price',thumb= '$file',descp ='$descp',pubtime = '$pubtime',addr='$addr',color ='$color',size='$size',ifshow='$show',sprice='$sprice',snums='$snums',pnums='$pnums',pimgs='$fiarr' where pid = $id";
	$res = mysql_query($sql);
	if(mysql_affected_rows()>0){
		msg('修改商品成功','product_list.php');
	}else{
		msg('修改商品失败！','product_list.php');
	}
}

//删除商品
else if($act == 'del'){
	//数据处理
	$id = $_GET['id'];
	$sql = "DELETE from tk_product where pid =$id";
	mysql_query($sql);
	if(mysql_affected_rows()>0){
		msg('删除成功','product_list.php');
	}else{
		msg('删除失败','product_list.php');
	}
}
?>